//
//  QKTabBar.m
//  DiandianManager
//
//  Created by 点点 on 2017/11/9.
//  Copyright © 2017年 DD. All rights reserved.
//

#import "QKTabBar.h"
//#import "UIView+QKExtension.h"
#import "UIView+Category.h"

static const NSInteger ITEMCOUNT = 5;
@interface QKTabBar ()

@property (nonatomic, strong) UIButton *orderButton;

@property (nonatomic, assign) NSInteger selectedIndex;

@end

@implementation QKTabBar

- (instancetype)init
{
    self = [super init];
    if (self) {
        self.backgroundColor = [UIColor whiteColor];
        self.layer.shadowColor = [UIColor colorWithRed:125/255.0 green:125/255.0 blue:125/255.0 alpha:0.1].CGColor;
        self.layer.shadowOffset = CGSizeMake(0,-3);
        self.layer.shadowOpacity = 1;
        self.layer.shadowRadius = 4;
        
        self.bgView = [[UIImageView alloc]initWithFrame:CGRectMake(0, 0, 72, 72)];
        self.bgView.center = CGPointMake(kScreenW/2.0, 0);
        self.bgView.backgroundColor = [UIColor whiteColor];
        self.bgView.layer.masksToBounds = YES;
        self.bgView.layer.cornerRadius = 36;
        self.bgView.contentMode = UIViewContentModeScaleAspectFit;
        [self addSubview:self.bgView];
        self.bgView.layer.shadowColor = [UIColor colorWithRed:125/255.0 green:125/255.0 blue:125/255.0 alpha:0.1].CGColor;
        self.bgView.layer.shadowOffset = CGSizeMake(0,-4);
        self.bgView.layer.shadowOpacity = 1;
        self.bgView.layer.shadowRadius = 4;
        
        self.imageV = [[UIImageView alloc]initWithFrame:CGRectMake(0, 0, 60, 60)];
        self.imageV.center = CGPointMake(kScreenW/2.0, 0);
        //imageV.backgroundColor = [UIColor redColor];
        self.imageV.image = [UIImage imageNamed:@"yuyin"];
        self.imageV.contentMode = UIViewContentModeScaleAspectFit;
        [self addSubview:self.imageV];

        self.label=[[UILabel alloc] initWithFrame:CGRectMake(kScreenW*2/5.0, 49-18, kScreenW/5.0, 18)];
        self.label.text=@"语音";
        self.label.font = PFRFont(12);
        self.label.textAlignment=NSTextAlignmentCenter;
        self.label.textColor = [UIColor dc_colorWithHexString:DC_646464];
        [self addSubview:self.label];
        
        UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
        button.frame = CGRectMake(kScreenW/2.0-kScreenW/10.0, -36, kScreenW/5.0, 85);
        button.adjustsImageWhenHighlighted = NO;
        button.backgroundColor = [UIColor clearColor];
        self.centerBtn = button;
        [self addSubview:button];
        
    }
    return self;
}

- (void)layoutSubviews
{
    [super layoutSubviews];

    self.label.font = PFRFont(12);
    
    for (UIView *sub in self.subviews) {
//        NSLog(@"===layoutSubviews:%@,%ld",sub,(long)sub.tag);
        if ([sub isKindOfClass:NSClassFromString(@"UITabBarButton")]) {
            for (UIView * subsub in sub.subviews) {
//                NSLog(@"===layoutSubviews1111111:%@,%ld",subsub,(long)subsub.tag);
                if ([subsub isKindOfClass:NSClassFromString(@"UITabBarButtonLabel")]) {
                    UILabel * text = (UILabel *)subsub;
                    if ([text.text isEqualToString:@"首页"]) {
                        sub.dc_width = kScreenW/ITEMCOUNT;
                        sub.center = CGPointMake((kScreenW/10.0), 49/2.0);
                    }else if ([text.text isEqualToString:@"分类"]) {
                        sub.dc_width = kScreenW/ITEMCOUNT;
                        sub.center = CGPointMake((kScreenW/10.0)*3, 49/2.0);
                    }else if ([text.text isEqualToString:@"购物车"]) {
                        sub.dc_width = kScreenW/ITEMCOUNT;
                        sub.center = CGPointMake((kScreenW/10.0)*7, 49/2.0);
                    }else if ([text.text isEqualToString:@"我的"]) {
                        sub.dc_width = kScreenW/ITEMCOUNT;
                        sub.center = CGPointMake((kScreenW/10.0)*9, 49/2.0);
                    }
                    text.center = CGPointMake(sub.center.x, 49-12);
                }
            }
        }
    }
}

- (UIView *)hitTest:(CGPoint)point withEvent:(UIEvent *)event {
    
    if (self.isHidden == NO) {
        
        CGPoint newPoint = [self convertPoint:point toView:self.centerBtn];
        
        if ( [self.centerBtn pointInside:newPoint withEvent:event]) {
            
            CAKeyframeAnimation *animation = [CAKeyframeAnimation animation];
            animation.keyPath = @"transform.scale";
            animation.values = @[@1.0,@1.1,@0.9,@1.0];
            animation.duration = 0.3;
            animation.calculationMode = kCAAnimationCubic;
            //添加动画
            [self.imageV.layer addAnimation:animation forKey:nil];
            
            return self.centerBtn;
            
        }else{
            return [super hitTest:point withEvent:event];
        }
    }
    else {
        return [super hitTest:point withEvent:event];
    }
}

@end
